﻿Public Class MenuManagement
    Inherits BasePage

    Protected dataSource As DataSet

    Private Sub MenuManagement_Init(sender As Object, e As EventArgs) Handles Me.Init
        Try
            Dim master As AdminMasterPage = DirectCast(Me.Page.Master, AdminMasterPage)
            master.CssLinkAdd(ResolveUrl("~/Style/MenuManagement.css"))

            master.ScriptLinkAdd(ResolveUrl("~/Scripts/MenuManagement.js"))
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
        Try
            If Not IsPostBack Then
                If Session("MenuA") IsNot Nothing Then                    
                    hdnMenuA.Value = Session("MenuA")
                Else
                    hdnMenuA.Value = 1
                End If
                hdnMenuB.Value = 0                
                GetMaster()
                GetMenu(hdnMenuA.Value)
                cbMenuA.SelectedValue = hdnMenuA.Value
            Else
                Dim menuA As Integer = Integer.Parse(hdnMenuA.Value)
                'cbMenuA.SelectedValue = menuA
                GetMenu(menuA)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub GetMaster()
        Dim xSql As String
        Dim da As DBAccess
        Dim ds As DataSet

        xSql = ""
        xSql = xSql & vbCrLf & "SELECT"
        xSql = xSql & vbCrLf & "		*"
        xSql = xSql & vbCrLf & "FROM"
        xSql = xSql & vbCrLf & "		T_CATEGORY"
        xSql = xSql & vbCrLf & ";"
        xSql = xSql & vbCrLf & "SELECT"
        xSql = xSql & vbCrLf & "		*"
        xSql = xSql & vbCrLf & "FROM"
        xSql = xSql & vbCrLf & "		T_MANUFACTORY"
        xSql = xSql & vbCrLf & ";"

        da = GetDBAccess()

        ds = da.GetDBDataset(xSql)

        If ds.Tables.Count > 1 Then
            cbMenuA.DataSource = ds.Tables(0)
            cbMenuB.DataSource = ds.Tables(1)
            Me.DataBind()
        End If

    End Sub

    Protected Overridable Sub GetMenu(Optional menuA As Integer = 1)
        Dim xSql As String
        Dim da As DBAccess        

        Try
            xSql = ""
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        MENU_A"
            xSql = xSql & vbCrLf & ",       MAX(NAME) NAME"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "        T_MENU "
            xSql = xSql & vbCrLf & "        LEFT JOIN T_CATEGORY "
            xSql = xSql & vbCrLf & "            ON MENU_A = CODE "
            xSql = xSql & vbCrLf & "GROUP BY"
            xSql = xSql & vbCrLf & "        MENU_A"
            xSql = xSql & vbCrLf & ";"
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        MENU_A"
            xSql = xSql & vbCrLf & ",       MENU_B"
            xSql = xSql & vbCrLf & ",       NAME"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "        T_MENU"
            xSql = xSql & vbCrLf & "        LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "            ON MENU_B = CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        MENU_A = {0}"

            xSql = String.Format(xSql, CommonMethods.ToSqlStr(menuA, CommonMethods.SqlDataType.DataType_Integer))

            da = GetDBAccess()

            dataSource = da.GetDBDataset(xSql)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnMenuA_Click(sender As Object, e As EventArgs) Handles btnMenuA.Click
        'Try
        '    Dim menuA As Integer = Integer.Parse(hdnMenuA.Value)            
        '    GetMenu(menuA)
        'Catch ex As Exception

        'End Try
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim xSql As String
        Dim da As DBAccess
        Dim menuA As Integer
        Dim menuB As Integer

        Try
            menuA = Integer.Parse(hdnMenuA.Value)
            menuB = Integer.Parse(hdnMenuB.Value)

            xSql = ""
            xSql = xSql & vbCrLf & "DELETE"
            xSql = xSql & vbCrLf & "        T_MENU"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        MENU_A = {0}"
            If menuB <> 0 Then
                xSql = xSql & vbCrLf & "AND     MENU_B = {1}"            
            End If

            If menuB <> 0 Then
                xSql = String.Format(xSql, CommonMethods.ToSqlStr(menuA, CommonMethods.SqlDataType.DataType_Integer), CommonMethods.ToSqlStr(menuB, CommonMethods.SqlDataType.DataType_Integer))
            Else
                xSql = String.Format(xSql, CommonMethods.ToSqlStr(menuA, CommonMethods.SqlDataType.DataType_Integer))
            End If

            da = GetDBAccess()

            da.ExecuteSqlString(xSql)

            If menuB = 0 Then
                hdnMenuA.Value = 1
                'GetMenu()
            Else
                'GetMenu(menuA)
            End If
            hdnMenuB.Value = String.Empty

            Session("MenuA") = menuA
            Response.Redirect(Request.RawUrl)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ComboBox_DataBound(sender As Object, e As EventArgs) Handles cbMenuA.DataBound, cbMenuB.DataBound        
        Try
            Dim control As DropDownList = DirectCast(sender, DropDownList)
            control.Items.Insert(0, New ListItem("--Chọn một--", 0))
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
        Try
            If IsExist() Then
                Response.Write("<script type='text/javascript'>alert('Menu mà bạn chọn đã tồn tại.');</script>")
            Else

                Dim xSql As String
                Dim da As DBAccess            

                xSql = String.Format("INSERT INTO T_MENU(MENU_A, MENU_B) VALUES({0}, {1})", _
                                     CommonMethods.ToSqlStr(cbMenuA.SelectedValue, CommonMethods.SqlDataType.DataType_Integer), _
                                     CommonMethods.ToSqlStr(cbMenuB.SelectedValue, CommonMethods.SqlDataType.DataType_Integer))

                da = GetDBAccess()

                da.ExecuteSqlString(xSql)

                Response.Write("<script type='text/javascript'>alert('Menu đã được save thành công.');</script>")
            End If

            Session("MenuA") = cbMenuA.SelectedValue
            Response.Redirect(Request.RawUrl)
        Catch ex As Exception

        End Try
    End Sub

    Protected Overridable Function IsExist() As Boolean
        Dim xSql As String
        Dim da As DBAccess
        Dim ds As DataSet

        xSql = ""
        xSql = xSql & vbCrLf & "SELECT"
        xSql = xSql & vbCrLf & "		MENU_A"
        xSql = xSql & vbCrLf & "FROM"
        xSql = xSql & vbCrLf & "		T_MENU"
        xSql = xSql & vbCrLf & "WHERE"
        xSql = xSql & vbCrLf & "		MENU_A = {0}"
        xSql = xSql & vbCrLf & "AND		MENU_B = {1}"

        xSql = String.Format(xSql, CommonMethods.ToSqlStr(cbMenuA.SelectedValue, CommonMethods.SqlDataType.DataType_Integer), _
                                   CommonMethods.ToSqlStr(cbMenuB.SelectedValue, CommonMethods.SqlDataType.DataType_Integer))

        da = GetDBAccess()

        ds = da.GetDBDataset(xSql)

        If ds.Tables(0).Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
End Class